from django.contrib import admin
from django.urls import reverse
from django.utils.html import format_html

from .models import Post, Comment


# Register your models here.

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    # 指定显示在修改页面上的字段
    list_display = ('id', 'title', 'slug', 'author', 'publish', 'status', 'operator')
    # 对列表元素进行过滤
    list_filter = ('status', 'created', 'publish', 'author',)
    # 添加一个搜索框
    search_fields = ('title', 'body',)
    # 设置预填充字段
    prepopulated_fields = {'slug': ('title',)}
    # 用于输入关联对象的主键id
    raw_id_fields = ('author',)
    # 根据你指定的日期相关的字段，为页面创建一个时间导航栏
    date_hierarchy = 'publish'
    # 设置排序的方式
    ordering = ('status', 'publish',)

    def operator(self, obj):
        return format_html(
            '<a href="{}">编辑</a>',
            reverse('admin:blog_post_change', args=(obj.id,))
        )
    operator.short_description = '操作'


@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
    list_display = ('id', 'name', 'email', 'post', 'created', 'active')
    list_filter = ('active', 'created', 'updated')
    search_fields = ('name', 'email', 'body')


admin.site.site_header = '博客管理'
admin.site.site_title = '博客后台系统'
